﻿#region Documentation
/*
 * Definition for the CategoryViewModel used to create entities of type Category.
 *  
 * Copyright (C) 2014  Daniel Hamacher
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 * 
 * */
#endregion

using SpendingMonitor.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpendingMonitor.ViewModels
{
    class CategoryViewModel : ViewModelBase
    {
        #region Properties

        private int id = 0;
        public int Id
        {
            get
            { return id; }

            set
            {
                if (id == value)
                { return; }

                id = value;
                RaisePropertyChanged("Id");
            }
        }

        private string name = string.Empty;
        public string Name
        {
            get
            { return name; }

            set
            {
                if (name == value)
                { return; }

                name = value;
                isDirty = true;
                RaisePropertyChanged("Name");
            }
        }        

        private bool isDirty = false;
        public bool IsDirty
        {
            get
            {
                return isDirty;
            }

            set
            {
                isDirty = value;
                RaisePropertyChanged("IsDirty");
            }
        }
        #endregion "Properties"


        #region CRUD
        public CategoryViewModel GetCategory(int categoryId)
        {
            var category = new CategoryViewModel();
            using (var db = new SQLite.SQLiteConnection(SpendingMonitor.App.DBPath))
            {
                var _category = (db.Table<Category>().Where(
                    c => c.Id == categoryId)).Single();
                category.Id = _category.Id;
                category.Name = _category.Name;               
            }
            return category;
        }       
        #endregion
    }
}
